// pages/record/record.js
var util = require('../../utils/util');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    end_date: '',
    start_date: '',
    user_id: '',
    content_id: ''
  },
  toPages(e){
    const that = this;
    var date = e.currentTarget.dataset.date;
    wx.navigateTo({
        url:'/pages/managePage/managePage?type=1&date='+date + '&content_id=' + that.data.content_id + '&user_id=' + that.data.user_id
    })
  },
  search() {
    wx.navigateTo({
      url: '/pages/recordSearch/recordSearch'
    })
  },
    getRecord(){
      var _this = this;
      const data = {
        start_date: _this.data.start_date,
        end_date: _this.data.end_date,
        user_id: _this.data.user_id,
        content_id: _this.data.content_id
      }
      console.log(data)
      util.getAjax(`${app.globalData.apiUrl}/Api/Food/get_order_list`,res=>{
          console.log('list', res);
          let count_normal = 0;
          let count_shu = 0;
          let count_qinzhen = 0;
          for(var i in res.data){
            var week = util.setWeek(res.data[i].date);
            var date = res.data[i].date;
            console.log(date);
            res.data[i].new_date = date;
            res.data[i].week = week;
            count_normal += Number(res.data[i].one)
            count_qinzhen += Number(res.data[i].two)
            count_shu += Number(res.data[i].zero)
          }
          _this.setData({
              recordList:res.data,
              count_normal:count_normal,
              count_qinzhen: count_qinzhen,
              count_shu: count_shu
          })
      }, data)
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const start = util.beforeAfterMonth().before;
    const end = util.beforeAfterMonth().today;
    this.setData({
      start_date: start,
      end_date: end
    })
    const that = this;
    if(Object.keys(options).length != 0) {
      console.log('if', options)
      that.setData({
        user_id: options.user_id,
        start_date: that.replaceFun(options.start),
        end_date: that.replaceFun(options.end),
        content_id: options.department_id
      })
    }
    this.getRecord();
    util.setNavBarTitle('订餐历史')
  },
  replaceFun(date) {
    return date.split('-').join('/')
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
